package com.spring.controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.spring.utils.Mail;

public class HomeController extends AbstractController {
	public static final int DEFAULT_SPITTLES_PER_PAGE = 25;

	private DataSource oracleDS;
	private JavaMailSender javaMailSender;
	private QueueViewMBean pexQueueBean;
	private Mail mail;

	public Mail getMail() {
		return mail;
	}

	public void setMail(Mail mail) {
		this.mail = mail;
	}
 
	public QueueViewMBean getPexQueueBean() {
		return pexQueueBean;
	}

	public void setPexQueueBean(QueueViewMBean pexQueueBean) {
		this.pexQueueBean = pexQueueBean;
	}

	public JavaMailSender getJavaMailSender() {
		return javaMailSender;
	}

	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}

	public DataSource getOracleDS() {
		return oracleDS;
	}

	public void setOracleDS(DataSource oracleDS) {
		this.oracleDS = oracleDS;
	}

	public HomeController() {
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println(javaMailSender.toString());

		Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
		ArrayList<String> arrayList = new ArrayList<String>();

		try {
			Connection conn = oracleDS.getConnection();
			Statement statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery("SELECT * FROM TAB");

			while (resultSet.next()) {
				arrayList.add(resultSet.getString(1));
			}

			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		map.put("tables", arrayList);

		mail.sendMail("rachitshah@gmail.com", "rachitshah@gmail.com", "Hello", "Mail From HelloSpring Controller!");

		return new ModelAndView("home", map);
	}

}
